#本代码为转载修改
#转载网址：https://blog.csdn.net/qq_42886289/article/details/115911308?ops_request_misc=&request_id=&biz_id=102&utm_term=用python制作gif动图&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-115911308.142^v68^js_top,201^v4^add_ask,213^v2^t3_esquery_v3&spm=1018.2226.3001.4187
import imageio
from pathlib import Path


def imgs2gif(imgPaths, saveName, duration=None, loop=0, fps=None):
    """
    生成动态图片 格式为 gif
    :param imgPaths: 一系列图片路径
    :param saveName: 保存gif的名字
    :param duration: gif每帧间隔， 单位 秒
    :param fps: 帧率
    :param loop: 播放次数（在不同的播放器上有所区别）， 0代表循环播放
    :return:
    """
    if fps:
        duration = 1 / fps
    images = [imageio.imread(str(img_path)) for img_path in imgPaths]
    imageio.mimsave(saveName, images, "gif", duration=duration, loop=loop)

inputpath = input("请输入图片地址：")
pathlist = Path(inputpath).glob("*.jpg")

p_lis = []
for n, p in enumerate(pathlist):
    if n % 5 == 0:
        p_lis.append(p)

imgs2gif(p_lis, "dance_0.gif", 0.033 * 5, 0)
imgs2gif(p_lis, "dance_1.gif", 0.033 * 5, 1)

#代码归属作者所有